package com.learn.atomic;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/16
 */
public class TestAtomicBasic {
    public static void main(String[] args) {

        AtomicInteger i = new AtomicInteger(5);

//        System.out.println(i.incrementAndGet());//++i 1
//        System.out.println(i.getAndIncrement());//i++ 2
//
//        System.out.println(i.getAndAdd(5));//2, 7
//        System.out.println(i.addAndGet(5));//12, 12
//
//        System.out.println(i.get());

//                       读取到的值  设置的值
//        i.updateAndGet(value -> value*10);

        while (true) {
//            获取i当前值
            int prev = i.get();
            int next = prev * 10;
            if(i.compareAndSet(prev,next)){
                break;
            }
        }

        i.getAndUpdate(value -> value * 10);//先获取旧值再运算设置新值
        System.out.println(i.get());

    }
}
